package top.zhangjianyong.blog.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.zhangjianyong.blog.entity.po.UserAgentInfo;
import top.zhangjianyong.blog.mapper.UserAgentInfoMapper;
import top.zhangjianyong.blog.record.UserAgentRecord;
import top.zhangjianyong.blog.service.UserAgentInfoService;
import top.zhangjianyong.blog.utils.UserAgentUtils;

/**
 * @author zhangjianyong
 * @since 2025/7/26 17:26
 */
@Service
public class UserAgentInfoServiceImpl extends ServiceImpl<UserAgentInfoMapper, UserAgentInfo>
    implements UserAgentInfoService {
    @Override
    public void saveOrUpdate(String userAgent) {
        if (StrUtil.isBlank(userAgent)) return;
        try {
            UserAgentInfo info = new UserAgentInfo();
            info.setUserAgent(userAgent);
            UserAgentRecord record = UserAgentUtils.parse(userAgent);
            info.setRecord(record);
            UserAgentInfo one = lambdaQuery()
                .eq(UserAgentInfo::getUserAgent, userAgent)
                .eq(UserAgentInfo::getIsDel, 0)
                .one();
            if (one == null) {
                this.save(info);
            } else {
                info.setId(one.getId());
                this.updateById(info);
            }
        } catch (Exception e) {
            log.error("保存用户代理信息失败：", e);
        }

    }
}
